#include "MCU_include.h"


void PWM_Safe_protection_Init(unsigned int PWM_Limit_MAX_Fr,unsigned int PWM_Limit_MIN_Fr,unsigned char PWM_Auto_Adj_DECPWMP) 
{
	unsigned int	TempMin,TempMax;
	
	TempMin = SysPWM_Frequency/PWM_Limit_MIN_Fr;
	TempMax = SysPWM_Frequency/PWM_Limit_MAX_Fr;	
	_pwmmaxpl = (TempMin - 1) & 0x0ff;
	_pwmmaxph = (TempMin - 1) >>8;   
	_pwmminpl = (TempMax - 1) & 0x0ff;
	_pwmminph = (TempMax - 1) >>8;	
	_decpwmp = PWM_Auto_Adj_DECPWMP;			
	_atmnpc = 1;  	//ATMNPC =1 

	_pwmpc0=0b11000010;		//		OVP4  limit PWM / OVP2  close PWM
	_pwmpc1=0b00000000;		
	_pwmpc2=0b00000000; 
	
	_plc=0x00;
	
	_ersgcs=0;			//
	_ersgs=0;			//	
	_ersgc=0b00111110;	////ovp6=>surge , OVP5/OVP3=>Resonant overcurrent second gear ，OVP4/OVP2=>Resonant overcurrent first gear ，bit0 Phase protection	
	_pmaxpte = 1;  		//PWM max value limit enable
	_nop();
}


void Phase_Protection_Init(unsigned char  PhaseSelect,unsigned char PWM_DelayPeriod,unsigned int Phase_Protect_sitatime_ns)
{
	unsigned long Temp;
	_phasess = PhaseSelect;
	_pwmc2  &= 0b10001101;
//	_pwmc2  &= 0b00000000;
	_pwmc2  |= PWM_DelayPeriod;	//Wait for PWM clock before starting phase detection
	Temp = (unsigned long)Phase_Protect_sitatime_ns*100;	
	Temp/=3125;
	_ppdsita = (unsigned char)(Temp-1);	
}


void PWM_MASK_Init(unsigned char  MASK0_Data,unsigned char  MASK1_Data)
{
	_msd1 = MASK1_Data;
	_msd0 = MASK0_Data;
}


void PWM_Freq_Init(unsigned char PWM_Clock_DIV,unsigned int Init_Fre)
{
//	unsigned int Temp;
	if (PWM_Clock_DIV==0)	PWM_SF	 = SysPWM_Frequency;
	else	PWM_SF = SysPWM_Frequency/PWM_Clock_DIV;
	PWM_F    = Init_Fre;
	switch(PWM_Clock_DIV)
	{
		case 2:
			_pwmck1 = 0; _pwmck0 = 1;
		break;
		case 4:
			_pwmck1 = 1; _pwmck0 = 0;
		break;
		case 8:
			_pwmck1 = 1; _pwmck0 = 1;
		break;
		default:
			_pwmck1 = 0; _pwmck0 = 0;
		break;
	}
	PWM_Update(PWM_F);
}


      
/**
  * @brief setting dead time.
  * @param[in] PWM0 dead time.
  * @param[in] PWM1 dead time.
  * can have one of the values from 0 to  65535 ns
  * @retval  none.
  */
void DTS_Setting(unsigned int DT0_time_ns,unsigned int DT1_time_ns)
{
	unsigned long DT_time_ns = 0;
	
    DT_time_ns = (unsigned long)DT0_time_ns*100;	
    DT_time_ns/=3125;
    _dt0l = (unsigned char)(DT_time_ns-1) & 0x0ff;
    _dt0h = (unsigned char)(DT_time_ns -1) >> 8;
    DT_time_ns = (unsigned long)DT1_time_ns*100;    
    DT_time_ns/=3125;
    _dt1l = (unsigned char)(DT_time_ns-1) & 0x0ff;
    _dt1h = (unsigned char)(DT_time_ns -1) >> 8;	
}

/**
  * @brief setting dead time.
  * @param[in] PWM0&PWM1 dead time.
  * can have one of the values from 0 to  65535 ns
  * @retval  none.
  */
void DTS0_1_Setting(unsigned int DT0_1_time_ns)
{
	unsigned long DT_time_ns = 0;
	
    DT_time_ns = (unsigned long)DT0_1_time_ns*100;	
    DT_time_ns/=3125;
    _dt0l = (unsigned char)(DT_time_ns - 1) & 0x0ff;
    _dt0h = (unsigned char)(DT_time_ns - 1) >> 8;
    _dt1l = (unsigned char)(DT_time_ns - 1) & 0x0ff;
    _dt1h = (unsigned char)(DT_time_ns - 1) >> 8;	
}

/****** 關閉pwm *******/
void IGBT_OFF(unsigned int PWM_Fr)
{
//	u16	Temp;
	while( PWM_Fr < MAX_FRE )
	{
		PWM_Fr = PWM_Fr + FRE_1000;
		if(PWM_Fr > MAX_FRE)	PWM_Fr = MAX_FRE;
		
		PWM_Update(PWM_Fr);		
		GCC_DELAY(20);
		GCC_CLRWDT();
	}
//	GCC_DELAY(100);
	PWM_OFF();
}

void IGBT_OFF1(unsigned int PWM_Fr)
{
//	u16	Temp;
	while( PWM_Fr < MAX_FRE )
	{
		PWM_Fr = PWM_Fr + FRE_1000;
		if(PWM_Fr > MAX_FRE)	PWM_Fr = MAX_FRE;
		
		PWM_Update1(PWM_Fr);
		GCC_DELAY(20);
		GCC_CLRWDT();
	}
//	GCC_DELAY(100);
	PWM_OFF();
}

//****** update pwm period and duty  *******//
void PWM_Update(unsigned int PWM_Fr)
{
	u16	Temp;
	
	Temp = PWM_SF/PWM_Fr;
	_pwmpl = (Temp-1) & 0x0ff;
	_pwmph = (Temp-1) >>8;
	_pwmdl = (Temp/2-1) & 0x0ff;
	_pwmdh = (Temp/2-1) >>8;	
}

//****** update pwm period and duty  *******//
void PWM_Update1(unsigned int PWM_Fr)
{
	u16	Temp;
	
	Temp = PWM_SF/PWM_Fr;
	_pwmpl = (Temp-1) & 0x0ff;
	_pwmph = (Temp-1) >>8;
	_pwmdl = (Temp/2-1) & 0x0ff;
	_pwmdh = (Temp/2-1) >>8;	
	
}

//****** update pwm MAX period and duty  *******//
//void PWMMAX_Update(unsigned int PWM_Fr)
//{
//	u16	Temp;	
//	Temp = PWM_SF/PWM_Fr;
//	_pwmmaxpl = (Temp-1) & 0x0ff;
//	_pwmmaxph = (Temp-1) >>8;
//
//}

//void PWMMAX_Update1(unsigned int PWM_Fr)
//{
//	u16	Temp;	
//	Temp = PWM_SF/PWM_Fr;
//	_pwmmaxpl = (Temp-1) & 0x0ff;
//	_pwmmaxph = (Temp-1) >>8;
//
//}


void ClearMSS0_1()
{
	_mss1 = 0;
	_mss0 = 0;
}

void ClearMSS0_1_for_ISR()
{
	_mss1 = 0;
	_mss0 = 0;
}



/******************* End of  file ***************/